<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrganizationModel
 *
 * @author wangnaixin
 */
namespace Admin\Model;
use Think\Model\RelationModel;
class OrganizationModel extends RelationModel {
    //put your code here
    
    protected  $tableName = 'organization';
    protected $_link = array(
        'user'=>array(
            'mapping_type' => self::MANY_TO_MANY,
            'class_name' =>'users',
            'mapping_name'=>'admins',
            'foreign_key'=>'org_id',
            'relation_foreign_key'=>'user_id',
            'relation_table'=>'user_org'
        ),
        'lease'=>array(
            'mapping_type' => self::HAS_MANY,
            'class_name' =>'Lease',
            'mapping_key' =>'id',
            'foreign_key'=>'org_id',
            'condition' =>'lease_status=\'0\'',
        ),
       /* 'attachment' => array(
    		'mapping_type' => self::HAS_MANY,
    		'class_name' => 'Attachment',
    		'foreign_key' => 'rel_id',
    		'mapping_key' => 'id',
            'relation_foreign_key'=>'house_id',
            'relation_table'=>'house',
    		'condition' => 'type=\'house\''
    	),*/
        'house'=>array(
            'mapping_type' => self::HAS_MANY,
            'class_name' =>'House',
            'mapping_key' =>'id',
            'foreign_key'=>'org_id',
            'condition' =>'status=\'1\'',
        ),
        'company' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'Company',
            'mapping_key' => 'id',
            'condition' => 'status=\'1\'',
            'foreign_key' => 'org_id',
        ),
        'province'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' =>'Region',
            'foreign_key'=>'province',
            'mapping_key' => 'id',
        ),
        'city'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' =>'Region',
            'foreign_key'=>'city',
            'mapping_key' => 'id',
        ),
        'county'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' =>'Region',
            'foreign_key'=>'county',
            'mapping_key' => 'id',
        ),

    );
    
    public function getOrgIds($orgs){
        $orgIds = [];
        foreach ($orgs as $v){
            $orgIds[] = $v['id'];

            $orgIds = array_merge($orgIds,$this->getOrgIdsByPid($v['id']));
        }
        $orgIds = array_unique($orgIds);
        return $orgIds;
    }
    
    public function getOrgIdsByPid($pid){
        $where = array(
            'pid'=>array('eq',$pid),
            'status'=>1
        );
        $res = $this->where($where)->field("id")->select();
        $orgIds = [];
        if(!empty($res) && $res !== false){
            foreach ($res as $v){
                $orgIds[] = $v['id'];
            }
        }
        return $orgIds;
    }
    
    public function getOrgListByIds($orgId,$relation=true,$pid=1){
        $where = array('status'=>1);
        if ($orgId != 0) {
            if ($orgId)  {
                $where['id'] = array('in',$orgId);
            } else {
                $where['id'] = 0;
            }
        }
        $row = array();
        if ($pid) {
            $where['pid'] = $pid;
            $row = $this->relation($relation)->where(array('status'=>1,'id'=>$pid))->find();
        }
        $count = $this->where($where)->count();
        $data = array();
        $rows = $this->relation($relation)->where($where)->select();
        if ($row) {
            $rows[] = $row;
        }
        foreach ($rows as $key => $item) {
            if ($item['pid'] == 0) {
                $data[] = $item;
                foreach ($rows as $subKey => $subItem) {
                    if ($item['id'] == $subItem['pid']) {
                        $subItem['name'] = sprintf('%s%s','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└─',$subItem['name']);
                        $data[] = $subItem;
                    }
                }
            }
        }
        $result = array(
            'total' => $count,
            'rows' => $data
        );
        return $result;
    }
    
    /**
     * 
     */
    
    public function getOrgById($orgId){
        $where = array(
            'id'=>array('eq',$orgId),
            'status'=>1
        );
        return $this->relation(true)->where($where)->find();
    }
    
    /**
     * @todo 逻辑删除组织机构
     * @param int $orgId
     * @return boolean
     */
    public function delOrgById($orgId){
        $where = array(
            'id'=>array('eq',$orgId)
        );
        $data = array(
            'status'=>0
        );
        return $this->where($where)->save($data);
    }
    
    public function getOrgInfoById($roleId){
        return $this->where("id = ".$roleId)->find();
    }

    //街道列表数据
    public function getOrgAllList($where,$relation=true){
        if ($relation) {
            $data = $this->relation(array('company','lease'))->where($where)->select();
        } else {
            $data = $this->where($where)->select();
        }
        return $data;
    }

    public function getTopOrgList($orgId) {
        $where = array('pid'=>0,'status'=>1);
        if ($orgId != 0) {
            if ($orgId)  {
                $where['id'] = array('in',$orgId);
            } else {
                $where['id'] = 0;
            }
        }
        return $this->where($where)->select();
    }


}
